/*
//	CP2013 Traffic Simulator Assignment :: (MVC) Controller (Header File)
//	Copyright (c) 2010, Adam Rehn
//
//	This is the main MVC Controller class, which is wrapped inside the main wxWidgets GUI frame.
*/

#ifndef _CONTROLLER_H
#define _CONTROLLER_H

//Include the model and view
#include "../model/model.h"
#include "../view/view.h"

//We are using SQLite 3 for our persistent storage of simulation results
#include <sqlite3.h>

//INSTANTIATING THE DATABASE CLASS WAS CRASHING THE PROGRAM!
//THE INLINE USE OF THE DATABASE FUNCTIONS IS TEMPORARY!

//Include the database access class
//#include "database.h"

class mvcController
{
	public:
		//Constructor and destructor
		mvcController(mvcModel* model);
		~mvcController();
		
		//Use this to register a view that will receive the updated model each time a cycle is run
		void addView(mvcView* view);
		
		//This will do the setup required before runCycle() can be called
		void startSimulation();
		
		//This will run one cycle
		bool runCycle(unsigned int i = 0, unsigned int t = 0);
		
		//This will do the cleanup and database commit after the cycles have been run
		void stopSimulation();
		
		//This will call startSimulation(), run the specified number of cycles, and then call stopSimulation()
		void runCycles(unsigned int num = 1);
		
	private:
		mvcModel *model;
		vector<mvcView*> views;
		
		//The application's persistent storage
		//simulationPersistentStorage* db;
		//dbAccess db;
		
		//The SQLite database handle
		sqlite3 *db;
		
		//Keeps track of the number of cars that pass through the intersection
		unsigned long long int numCarsPassedThroughIntersection;
		
		//Keeps track of the number of cycles that have been run
		unsigned long long int numCyclesRun;
		
		//This will check if a specific tile is obstructed
		bool isObstructed(coordinate t, direction_t dir = DIRECTION_UNDEFINED);
		
		//This will check if a range of tiles are obstructed
		bool isObstructed(coordinate base, signed int w, signed int h, direction_t dir = DIRECTION_UNDEFINED);
		
		//This will check if a specific vehicle is trying to drive off of the map
		bool drivingOffTheEdgeOfTheWorld(vehicle& v);
		
		//This will check if a specific vehicle is entering the intersection
		bool isEnteringIntersection(vehicle& v);
		
		//This will remove a vehicle from memory
		void removeVehicle(unsigned int i);
		
		//This will alter the directions of all of the tiles in the intersection to match the state of the traffic lights
		void updateIntersectionTiles();
};

#endif
